package com.lanou.bookstore.order.dao;

import com.lanou.bookstore.book.dao.BookDao;
import com.lanou.bookstore.book.domain.Book;
import com.lanou.bookstore.order.domain.Order;
import com.lanou.bookstore.order.domain.OrderItem;
import com.lanou.bookstore.util.JDBCUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;


public class OrderDao {


    // 添加订单
    public void addOrder(Order order) {

        String sql = "INSERT INTO ORDERS(oid, ordertime, total, state, uid) VALUES(?,?,?,?,?)";
        Connection conn = JDBCUtil.getConn();

        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, order.getOid());
            statement.setString(2, order.getOrderTime());
            statement.setDouble(3, order.getTotal());
            statement.setInt(4, order.getState());
            statement.setString(5, order.getUid());
            statement.executeUpdate();
            statement.close();
        } catch (SQLException e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }

    }


    public List<Order> findByUid(String uid) throws SQLException {
        List<Order> orders = new ArrayList<>();
        String sql = "select * from orders o,orderitem i,book b where o.oid=i.oid and i.bid=b.bid and o.uid=?";
        Connection conn = JDBCUtil.getConn();
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setString(1, uid);
        ResultSet rs = statement.executeQuery();
        while (rs.next()) {
            boolean flag = false;
            for (Order order : orders) {
                if (order.getOid().equals(rs.getString("oid"))) {
                    flag = true;
                    Book book = new Book();
                    book.setBid(rs.getString("bid"));
                    book.setBname(rs.getString("bname"));
                    book.setPrice(rs.getFloat("price"));
                    book.setAuthor(rs.getString("author"));
                    book.setImage(rs.getString("image"));
                    book.setCid(rs.getString("cid"));

                    OrderItem orderItem = new OrderItem();
                    orderItem.setIid(rs.getString("iid"));
                    orderItem.setCount(rs.getInt("count"));
                    orderItem.setSubtotal(rs.getDouble("subtotal"));
                    orderItem.setBid(rs.getString("bid"));
                    orderItem.setOid(rs.getString("oid"));
                    orderItem.setBook(book);

                    order.getOrderItemList().add(orderItem);
                    break;
                }
            }

            if (!flag) {
                Order order = new Order();
                order.setOid(rs.getString("oid"));
                order.setOrderTime(rs.getString("ordertime"));
                order.setTotal(rs.getDouble("total"));
                order.setState(rs.getInt("state"));
                order.setUid(rs.getString("uid"));
                order.setAddress(rs.getString("address"));

                Book book = new Book();
                book.setBid(rs.getString("bid"));
                book.setBname(rs.getString("bname"));
                book.setPrice(rs.getFloat("price"));
                book.setAuthor(rs.getString("author"));
                book.setImage(rs.getString("image"));
                book.setCid(rs.getString("cid"));

                OrderItem orderItem = new OrderItem();
                orderItem.setIid(rs.getString("iid"));
                orderItem.setCount(rs.getInt("count"));
                orderItem.setSubtotal(rs.getDouble("subtotal"));
                orderItem.setBid(rs.getString("bid"));
                orderItem.setOid(rs.getString("oid"));
                orderItem.setBook(book);

                order.getOrderItemList().add(orderItem);

                orders.add(order);
            }
        }
//            while (rs.next()) {
//                Order order = new Order();
//                setOrderParam(order, rs);
//                orders.add(order);
//            }
//            rs.close();
//            statement.close();
//
//        return orders;
        return orders;
    }


    public void addOderItemList(List<OrderItem> orderItemList) {
        Connection conn = JDBCUtil.getConn();
        try {

            String sql = "INSERT INTO ORDERITEM (iid, count, subtotal, oid, bid) VALUES(?,?,?,?,?)";
            for (OrderItem orderItem : orderItemList) {
                PreparedStatement statement = conn.prepareStatement(sql);
                statement.setString(1, orderItem.getIid());
                statement.setInt(2, orderItem.getCount());
                statement.setDouble(3, orderItem.getSubtotal());
                statement.setString(4, orderItem.getOid());
                statement.setString(5, orderItem.getBid());
                statement.executeUpdate();
                statement.close();
            }
        } catch (Exception e) {
            try {
                conn.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }


    }


    private List<OrderItem> findByOid(String oid) {
        BookDao bookDao = new BookDao();
        List<OrderItem> list = new ArrayList<>();
        String sql = "select * from orderitem where oid = ?";
        Connection conn = JDBCUtil.getConn();
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, oid);
            ResultSet rs = statement.executeQuery();
            while (rs.next()) {
                OrderItem orderItem = new OrderItem();
                orderItem.setIid(rs.getString("iid"))
                        .setCount(rs.getInt("count"))
                        .setSubtotal(rs.getDouble("subtotal"))
                        .setOid(rs.getString("oid"))
                        .setBid(rs.getString("bid"));
                Book book = bookDao.load(orderItem.getBid());
                orderItem.setBook(book);
                list.add(orderItem);
            }
            rs.close();
            statement.close();

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    public Order load(String oid) {
        Order order = new Order();
        List<OrderItem> orderItemList = findByOid(oid);
        String sql = "select * from orders where oid = ?";
        Connection conn = JDBCUtil.getConn();
        try {
            PreparedStatement statement = conn.prepareStatement(sql);
            statement.setString(1, oid);
            ResultSet rs = statement.executeQuery();
            rs.next();
            order.setOid(rs.getString("oid"))
                    .setOrderTime(rs.getString("ordertime"))
                    .setTotal(rs.getDouble("total"))
                    .setState(rs.getInt("state"))
                    .setUid(rs.getString("uid"))
                    .setOrderItemList(orderItemList);
            rs.close();
            statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return order;
    }

    public void upDateState(String oid, int state) throws SQLException {
        String sql = "update orders set state = ? where oid = ?";
        Connection conn = JDBCUtil.getConn();
        PreparedStatement statement = conn.prepareStatement(sql);
        statement.setInt(1, state);
        statement.setString(2, oid);
        statement.executeUpdate();
        statement.close();
    }


}